package com.prodavalnik.controller;


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import javax.servlet.http.HttpSession;

import com.prodavalnik.model.DataBaseController;
import com.prodavalnik.model.User;

/**
 * Servlet implementation class ProfileServlet
 */
@WebServlet("/ProfileServlet")
public class ProfileServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProfileServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession(false);
		PrintWriter writer = response.getWriter();

		if(session == null){
			writer.write("error");
			return;
		}
		String username = (String) session.getAttribute("username");
		String password = (String) session.getAttribute("password");

		User user = null;
		try {
			user = DataBaseController.returnUserByUsernameAndPassword(username,password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		if(user.getConn()!=null)
			writer.write(user.getEmail()+","+user.getConn());
		else
			writer.write(user.getEmail()+""+""+"");

	}
	

}
